package com.nightpiter.view.drawer.dynamic;

import static com.nightpiter.view.drawer.rapid.Config.transferWidth;
import static com.nightpiter.view.drawer.rapid.Config.tunnelWidth;

import java.util.ArrayList;

import com.nightpiter.model.objects.Transfer;
import com.nightpiter.model.objects.Tunnel;
import com.nightpiter.view.Calculator;
import com.nightpiter.view.drawer.rapid.Config;
import com.nightpiter.view.graphics.Canvas;
import com.nightpiter.view.graphics.Paint;
import com.nightpiter.view.graphics.PointF;
import com.nightpiter.view.graphics.abstaractions.AbstractPaint.Style;

/**
 * @author Eldar Abusalimov (eldar.abusalimov@gmail.com)
 * 
 */
abstract class Bacillus {

	/**
	 * @author Eldar Abusalimov (eldar.abusalimov@gmail.com)
	 * 
	 */
	protected class TunnelPart {

		public boolean opposite = false;

		public boolean singleSide = true;

		public int color;

		public Tunnel tunnel;

	}

	/**
	 * @author Eldar Abusalimov (eldar.abusalimov@gmail.com)
	 * 
	 */
	protected class TransferPart {

		public boolean opposite = false;

		public Transfer transfer;

	}

	protected IntermediateBacillus intermediateBacillus;

	protected Amoeba evenAmoeba, oddAmoeba;

	protected ArrayList<TransferPart> transferParts = new ArrayList<TransferPart>();

	protected ArrayList<TunnelPart> tunnelParts = new ArrayList<TunnelPart>();

	protected int tunnelsSize = 0, transfersSize = 0;

	protected static Paint tunnelPaint = new Paint();

	protected static Paint transferPaint = new Paint();

	protected void draw(Canvas canvas, Calculator calculator) {
		PointF evenPointF = this.evenAmoeba.pointF;
		PointF oddPointF = this.oddAmoeba.pointF;
		PointF normal = PointF.difference(evenPointF, oddPointF);
		normal.rotateCW();
		normal.normalize();
		normal.scale(tunnelWidth);
		if (tunnelsSize == 1) {
			tunnelPaint.setColor(tunnelParts.get(0).color);
			canvas.drawLine(evenPointF, oddPointF, tunnelPaint);
		} else {
			PointF offset = new PointF(normal);
			offset.scale((float) (1 - tunnelsSize) / 2);
			for (TunnelPart tunnelPart : tunnelParts) {
				tunnelPaint.setColor(tunnelPart.color);
				canvas.drawLine(PointF.sum(evenPointF, offset), PointF.sum(
						oddPointF, offset), tunnelPaint);
				offset.add(normal);
			}
		}
		for (@SuppressWarnings("unused") TransferPart transferPart : transferParts) {
			canvas.drawLine(evenPointF, oddPointF, transferPaint);
		}		
	}

	public static void setup() {
		tunnelPaint.setStrokeWidth(tunnelWidth);
		transferPaint.setStrokeWidth(transferWidth);
	}

	static {

		tunnelPaint.setAntiAlias(true);
		tunnelPaint.setStrokeCap(Paint.Cap.ROUND);

		transferPaint.setAntiAlias(true);
		transferPaint.setStrokeCap(Paint.Cap.BUTT);
		transferPaint.setStyle(Style.FILL_AND_STROKE);
		transferPaint.setColor(Config.DEFAULT_COLOR);

	}

}
